<?php
//建造者模式

/** 
 * 建造者模式
 * copyright (c) http://blog.csdn.net/CleverCode   
 */  
 
//需要建造的产品(product)
class Car{
    public $name;
    public $engine;//发送机
    public $chassis;//地盘
    public $body;//车身
    public $equipment;//电器设备

    public function setName($name){
        $this->name = $name;
    }
    public function setEngine($engine){
        $this->engine = $engine;
    }
    public function setChassis($chassis){
        $this->chassis = $chassis;
    }
    public function setBody($body){
        $this->body = $body;
    }
    public function setEquipment($equipment){
        $this->equipment = $equipment;
    }
    public function show(){
        $ln = "\r\n";
        echo "名称：" . $this->name . $ln;
        echo "引擎：" . $this->engine . $ln;
        echo "底盘：" . $this->chassis . $ln;
        echo "车身：" . $this->body . $ln;
        echo "电子设备：" . $this->equipment . $ln;
    }
}
//builder
interface IBuilder {
    public function builderName();
    public function builderEngine();
    public function builderChassis();
    public function builderBody();
    public function builderEquipment();
    public function getCar();
}
//红旗车builder
class RedBuilder implements IBuilder{
    public $car;
    public function __construct(){
        $this->car = new Car();
    }
    public function builderName(){
        $this->car->setName("红旗");
    }
    public function builderEngine(){
        $this->car->setEngine("国产发动机");
    }
    public function builderChassis(){
        $this->car->setChassis("超大底盘");
    }
    public function builderBody(){
        $this->car->setBody("超大超长车身，天窗");
    }
    public function builderEquipment(){
        $this->car->setEquipment("电子设备");
    }
    public function getCar(){
        return $this->car;
    }
}
//QQ车builder
class QQBuilder implements IBuilder{
    public $car;
 
    public function __construct(){
        $this->car = new Car();
    }
 
    public function builderName(){
        $this->car->setName('QQ'); 
    }
 
    public function builderEngine(){
        $this->car->setEngine('国产发动机'); 
    }
 
    public function builderChassis(){
        $this->car->setChassis('小底盘'); 
    }
 
    public function builderBody(){
        $this->car->setBody('小'); 
    }
    public function builderEquipment(){
        $this->car->setEquipment('电子设备'); 
    }
 
    public function getCar(){
        return $this->car;
    }
}

//组织者
class CarDirector{
    public function make(IBuilder $builder){
        $builder->builderName();
        $builder->builderEngine();
        $builder->builderChassis();
        $builder->builderBody();
        $builder->builderEquipment();
        return $builder->getCar();
    }
}
class Client{
    public static function main($argv){
        $director = new CarDirector();
        $redBuilder = new RedBuilder();
        $car = $director->make($redBuilder);
        $car->show();

        echo "\r\n";
        $qqBuilder = new QQBuilder();
        $car = $director->make($qqBuilder);
        $car->show();
    }
}
Client::main($argv);